#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QFile>
#include <QDebug>
#include <QDesktopWidget>
#include <QMessageBox>
#include <QTextEdit>

#include "xmlsyntaxhighlighter.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);    

    trayIcon = new QSystemTrayIcon();
    trayIcon->setVisible(true);

    connectAll2All();

    m_DefaultPool = new CardPool();
    ui->listViewCardsList->setModel(m_DefaultPool->model);

    LoadPoolFromFile();
}

void MainWindow::connectAll2All()
{
    connect(ui->btnSavePool, SIGNAL(clicked()), this, SLOT(btnSavePoolClicked()));
    connect(ui->btnRemoveCardFromPool, SIGNAL(clicked()), this, SLOT(btnRemoveCardFromPoolClicked()));
    connect(ui->btnAddCardToPool, SIGNAL(clicked()), this, SLOT(btnAddCardToPoolClicked()));

    connect(ui->listViewCardsList, SIGNAL(doubleClicked(QModelIndex)), SLOT(listviewCardListDoubleClicked(QModelIndex)));
    connect(ui->actionAbout_QT, SIGNAL(triggered()), this, SLOT(aboutQT()));
}

void MainWindow::aboutQT()
{
    QMessageBox::aboutQt(0);
}

void MainWindow::LoadPoolFromFile()
{

//    QFile file("./mtg_deck_default.xml");
//    QString text;
//    if (file.open(QIODevice::ReadOnly))
//    {
//        QTextStream in(&file);
//        while (!in.atEnd()) {
//            QString line = in.readLine();
//            text.append(line);
//        }
//        file.close();
//    }
//
//    QDomDocument *doc;
//    doc = new QDomDocument("MTG");
//    doc->setContent(text);
//    m_DefaultPool->fromXmlDefinition(doc);
    m_DefaultPool->loadFromFile("./mtg_deck_default.xml");

}


MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::listviewCardListDoubleClicked(QModelIndex node)
{
    QDialog * dialog = new QDialog(this);
    QVBoxLayout *layout = new QVBoxLayout();
    QTextEdit *ed = new QTextEdit();
    ed->setPlainText(node.data(Qt::UserRole).toString());
    QPushButton *button = new QPushButton();
    button->setText("Close");
    connect(button, SIGNAL(clicked()), dialog, SLOT(close()));

    new XmlSyntaxHighlighter(ed->document());

    layout->addWidget(ed);
    layout->addWidget(button);
    dialog->setLayout(layout);

    dialog->setWindowTitle("Edit card");
    dialog->setGeometry(QRect(20, 20, 600, 800));
    dialog->exec();
    m_DefaultPool->changeCard(node, ed->toPlainText(), Qt::UserRole);
}

void MainWindow::btnSavePoolClicked()
{
    QDomDocument *doc;
    doc = new QDomDocument("MTG");
    doc->setContent(m_DefaultPool->toXmlDefinition());

    QFile file("./mtg_deck_default.xml");
    if (file.open(QIODevice::WriteOnly))
    {
        QTextStream(&file) << doc->toString(4);
        file.close();

        trayIcon->showMessage("Card Pool Information", "Successfully saved");
    }
}

void MainWindow::btnRemoveCardFromPoolClicked()
{
    if (!ui->listViewCardsList->currentIndex().isValid())
        return;

    m_DefaultPool->RemoveCard(ui->listViewCardsList->currentIndex().row());
}

void MainWindow::btnAddCardToPoolClicked()
{
    QDialog *dialog = new QDialog(this);
    QVBoxLayout *layout = new QVBoxLayout;
    QPushButton *btn = new QPushButton();
    dialog->setLayout(layout);
    FormNewCard *form = new FormNewCard(this);
    layout->addWidget(form);
    layout->addWidget(btn);

    connect(btn, SIGNAL(clicked()), dialog, SLOT(close()));
    btn->setText("Close");
    dialog->setWindowState(Qt::WindowMaximized);
    dialog->exec();
    form->SaveNewCard();
    m_DefaultPool->AddCard(form->card);
}
